<?php
class shopapiControl extends skymvc{
	public $userid;
	public function __construct(){
		parent::__construct();
	}
	
	public function onInit(){
		$this->userid=M("login")->userid;
	}
	public function onList($option=array()){
		$data=M("shop")->select($option);
		if($data){
			$favshopids=$this->favshopids();
			foreach($data as $k=>$v){
				if($favshopids && in_array($v['shopid'],$favshopids)){
					$v['isfav']=1;
				}else{
					$v['isfav']=0;
				}
				
				 
				
				$data[$k]=$v;
			} 
		}
		return $data;
	}
	
	public function get($shopid){
		$shopid=max(1,$shopid);
		$data=M("shop")->selectRow("shopid=".intval($shopid));
		if(empty($data)) return false;
		if($this->userid){			
			if($r=M("love")->selectRow(array("where"=>" object_id=".$data['shopid']."  tablename='shop' "))){
				$data['isfav']=1;
			}
		}
		//判断是否营业中
		 
		return $data;
	}
	/**
	我收藏的店铺
	*/
	public function fav($limit=10){
		if(!$this->userid) return false;
		$ids=$this->favshopids();
		if(empty($ids)) return array();
		$option=array(
			"where"=>"shopid in("._implode($ids).")",
			"limit"=>$limit
		);
		$data=M("shop")->select($option);
		return $data;
	}
	
	public function UnRecommendshop($sc_id=0,$limit=100){
		$sc_id=intval($sc_id);
		$where=" isrecommend=0 AND status=1 ";
		if($sc_id){
			$ids=M("site_city")->id_family($sc_id);
			if($ids){
				$where.=" AND sc_id in("._implode($ids).")";
			}else{
				$where.=" AND 1=2 ";
			}
		}
		$option=array(
			"where"=>$where,
			"limit"=>$limit
		);
		return $data=$this->onList($option);
		 
	}
	
	public function RecommendShop($limit=100){
		 
		$where=" isrecommend=1 AND status=1 ";
		 
		$option=array(
			"where"=>$where,
			"limit"=>$limit
		);
		return $data=$this->onList($option);
	}
		
 	/*附近的商家*/
 	public function nearShop($option=array()){
		$meter=0.00001*1.1;//1米以内
		$meter=isset($_GET['mi'])?$meter*get('mi'):$meter*10000;
		$mi=get('mi','i');
		$miarr=array();
		$latlng=explode("-",$_COOKIE['latlng']);
		$lat=round($latlng[0],6);
		$lng=round($latlng[1],6);
		if($lat>0)
		{
			$ilng=$lng+$meter;
			$mlng=$lng-$meter;
			$ilat=$lat+$meter;
			$mlat=$lat-$meter;
			$pagesize=12;
			$page=max(1,intval($_GET['page']));
			$start=($page-1)*$pagesize;
			$where="  (lng<'$ilng' AND lng>'$mlng') AND (lat>'$mlat' AND lat<'$ilat') ";
			$option=array(
				"where"=>$where,
				"limit"=>2000,
				"fields"=>"shopid,(ABS(lat-".$lat.") + ABS(lng-".$lng.")) as mi",
			);
			$rscount=true;
			$arr=M("shop")->select($option,$rscount);
			if($arr)
			{
				foreach($arr as $r)
				{
					if($r['mi']<$meter)
					{
						$miarr[]=$r;
						$ids[]=$r['shopid'];
					}
				}
			}
			
			$start=get('per_page','i');
			$limit=20;
			if($ids){
				$max=count($ids);
				$big=$start+$limit;
				$big=$big<$max?$big:$max;
				for($i=$start;$i<$big;$i++){
					$tids[]=$ids[$i];
				}
				if($tids){
					return $data=$this->onList(array("where"=>" shopid in("._implode($tids).")"));
				}
			}
		}
		
	}
	
	public function favshopids($sc_id=0){
		if(!$this->userid) return false;
		$sc_id && $scids=M("site_city")->id_family($sc_id);
		$sc_in="";
		if($scids){
			$sc_in=" AND sc_id in("._implode($scids).")";
		} 
		return M("love")->selectCols(array("where"=>" tablename='shop' AND userid=".$this->userid." $sc_in ","fields"=>"object_id"));
	}
	
	
	
	public function HotProduct($shopid,$limit=10){
		$option=array(
			"where"=>" shopid=$shopid AND status<98 ",
			"limit"=>$limit
		);
		$data=M("product")->select($option);
		return $data;
	}
	
	public function favProduct($shopid,$limit=10){
		if(!$this->userid) return false;
		$favids=M('fav')->selectCols(array("where"=>"userid=".$this->userid,"fields"=>"object_id"));
		$proids=M("product")->selectCols(array("where"=>" shopid=".$shopid,"fields"=>"id"));
		$where="  shopid=$shopid  ";
		if(empty($favids) or empty($proids)){
			$where.=" AND 1=2 ";
		}else{
			$ids=array_intersect($favids,$proids);
			$ids && $where.=" AND id in("._implode($ids).") ";
		}
		$option=array(
			"where"=>$where,
			"limit"=>$limit
		);
		$data=M("product")->select($option);
		return $data;
	}
	
}
?>